package com.mes.check.dao;

import org.springframework.stereotype.Repository;

import com.bstek.dorado.data.provider.Page;
import com.mes.check.dao.iface.IDefectJudgeDao;
import com.mes.check.domain.DefectJudge;
import com.mes.common.dao.BasicDao;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;

@Repository
public class DefectJudgeDao extends BasicDao<DefectJudge> implements IDefectJudgeDao{
	private static final Log log = LogFactory.getLog(DefectJudgeDao.class);
	
	@Override
	public void getDefectJudgesByConditions(Page<DefectJudge> page, Map<String, Object> params) throws Exception {
		Map<String, Object> args = new HashMap<String, Object>();
		StringBuffer coreHql = new StringBuffer("from "
				+ DefectJudge.class.getName() + " a where a.checker.deptUid like '001700%' ");

		String drawingId = (String) params.get("drawingId");
		if (StringUtils.isNotEmpty(drawingId)) {
			coreHql.append(" and a.operationTask.part.drawingId like :drawingId ");
			args.put("drawingId", "%" + drawingId + "%");
		}
		
		String batchNum = (String) params.get("batchNum");
		if (StringUtils.isNotEmpty(batchNum)) {
			coreHql.append(" and a.operationTask.batchNum like :batchNum ");
			args.put("batchNum", "%" + batchNum + "%");
		}
		
		String billNumber = (String) params.get("billNumber");
		if (StringUtils.isNotEmpty(billNumber)) {
			coreHql.append(" and a.billNumber like :billNumber ");
			args.put("billNumber", "%" + billNumber + "%");
		}
		
		Integer billState = (Integer) params.get("billState");
		if (billState != null) {
			if(billState == 9){
				coreHql.append(" and a.judgeBillState <= :billState ");
			}else{
				coreHql.append(" and a.judgeBillState = :billState ");
			}
			args.put("billState", billState);
		}
		
		Boolean isInEffect = (Boolean) params.get("isInEffect");
		if (isInEffect != null) {
			coreHql.append(" and a.isInEffect = :isInEffect ");
			args.put("isInEffect", isInEffect);
		}
		
		String countHql = "select count(*) " + coreHql.toString();
		String hql = coreHql.toString();
		
		Map<String, Object> newArgs = new HashMap<String, Object>();
		String key;
		String newKey;
		Iterator<String> iterator = args.keySet().iterator();
		while(iterator.hasNext()){
			key = (String)iterator.next();
			newKey = key.replace(".", "_");
			newArgs.put(newKey, args.get(key));
			hql = hql.replace(":"+key, ":"+newKey);
			countHql = countHql.replace(":"+key, ":"+newKey);
		}
		this.pagingQuery(page, hql, countHql, newArgs);
	}
	
	
	@Override
	public List<DefectJudge> getDefectJudgesByTaskUid(String taskUid) {
		List list = null;
		try {
			log.info("++++++++++++++++++++++++++++++++++++getDefectJudgesByTaskUid");
			Session session=this.getSession();
			StringBuilder hql=new StringBuilder();
			hql.append(" from " + this.getClass().getName() + " where ");
			hql.append( " taskUid=? ");
			Query query = session.createQuery(hql.toString());
			query.setString(0, taskUid);
			
			list = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
}
